//
//  BVHomeNormalTableViewCell.m
//  AFNetworking
//
//  Created by xiute on 2019/10/15.
//

#import "BVHomeNormalTableViewCell.h"
#import "UIImageView+BVWebCache.h"
#import "UIView+BV.h"
#import "UILabel+BV.h"
#import <SVProgressHUD/SVProgressHUD.h>
#import "BVService.h"

@interface BVHomeNormalTableViewCell ()


@property (weak, nonatomic) IBOutlet UIView *goalContainerView;
@property (weak, nonatomic) IBOutlet UILabel *goalLabel;
@property (weak, nonatomic) IBOutlet UIView *topContainerView;

@property (weak, nonatomic) IBOutlet UIButton *favoriteButton;

@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (nonatomic, strong) BVMetaModel *metaModel;
@end

@implementation BVHomeNormalTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.backgroundColor = [UIColor whiteColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.topContainerView.layer.cornerRadius = 11;
    self.topContainerView.layer.masksToBounds = YES;
    self.topContainerView.clipsToBounds = YES;
    self.topContainerView.backgroundColor = [UIColor whiteColor];
    
    self.contentView.layer.shadowColor = [UIColor colorWithWhite:0 alpha:0.2].CGColor;
    self.contentView.layer.shadowOffset = CGSizeMake(10, 10);
    self.contentView.layer.shadowRadius = 4;
    self.contentView.layer.shadowOpacity = 10;
    
    
    self.backgroundColor = [UIColor whiteColor];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.clipsToBounds = NO;
    self.layer.masksToBounds = NO;
    self.contentView.clipsToBounds = NO;
    self.contentView.layer.masksToBounds = NO;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    self.goalContainerView.backgroundColor = [UIColor clearColor];
    self.goalContainerView.layer.cornerRadius = 11;
    
    
    
    
    [self.goalLabel setCustomFont];
}

- (void)configWithModel:(BVMetaModel *)model indexPath:(nonnull NSIndexPath *)indexPath{
    self.metaModel = model;
    [self.backImageView bv_imageWithUrl:model.cover];
    self.goalLabel.text = [NSString stringWithFormat:@"GOAL %ld", (long)indexPath.row];
    if (indexPath.row % 2 == 0) {
        self.goalContainerView.backgroundColor = [UIColor colorFromHexString:@"#05508D"];
    }else {
        self.goalContainerView.backgroundColor = [UIColor colorFromHexString:@"#e0623e"];
    }
    
    if (model.likes) {
        [self.favoriteButton setImage:[UIImage bv_imageNamed:@"video_favorite_icon_home_red" bundle:[BVBundle bundleWithClass:self.class name:BV_BUNDLE_HOME_SCENE_MODULE]] forState:UIControlStateNormal];
    }else {
        [self.favoriteButton setImage:[UIImage bv_imageNamed:@"video_favorite_icon_home_red_not_fav" bundle:[BVBundle bundleWithClass:self.class name:BV_BUNDLE_HOME_SCENE_MODULE]] forState:UIControlStateNormal];
    }
}

- (IBAction)shareButtonAct:(UIButton *)sender {
    if (self.shareActBlock) {
        self.shareActBlock();
    }
}

- (IBAction)favoriteButtonAct:(UIButton *)sender {
    self.favoriteAct ? self.favoriteAct(): nil;
}

- (IBAction)downloadButtonAct:(UIButton *)sender {
    [SVProgressHUD showProgress:0];
    [[BVService shareInstance] downloadVideoWithUrl:self.metaModel.video progress:^(NSProgress * _Nonnull progress) {
        [SVProgressHUD showProgress:(double)progress.completedUnitCount / (double)progress.totalUnitCount];
    } complete:^(BOOL success, NSURL * _Nullable filePath) {
        [SVProgressHUD dismiss];
        if (success) {
            NSString *filePathStr = [filePath.absoluteString stringByReplacingOccurrencesOfString:@"file://" withString:@""];
            UISaveVideoAtPathToSavedPhotosAlbum(filePathStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
        }else {
            [SVProgressHUD showErrorWithStatus:@"保存失败 请重试"];
        }
    }];
}

- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
    if (error) {
        [SVProgressHUD showErrorWithStatus:@"保存失败 请重试"];
    }
    else {
        [SVProgressHUD showSuccessWithStatus:@"保存视频成功"];
    }
  
}

- (IBAction)editButtonAct:(UIButton *)sender {
    if (self.editAct) {
        self.editAct();
    }
}


+ (CGFloat)cellHeight {
    return (kSCREEN_WIDTH - 48) * 0.5625 + 40 + 25;
}

@end
